package com.touni.datastructure;

import com.sun.org.apache.regexp.internal.RE;

/**
 * @ClassName : LinkedList
 * @Description :
 * @Author : lwq
 * @Date: 2021-04-23 16:14
 */
public class LinkedList<T> {

    private Node<T> head;

    private Node<T> tail;

    class Node<T> {
        T value;
        Node<T> next;
    }

    public boolean add(T value){
        Node<T> node = new Node<>();
        node.value = value;
        if (head == null){
            head = node;
        }
        if (tail != null){
            tail.next = node;
        } else {
            head.next = node;
        }

        return Boolean.TRUE;
    }

    public boolean delete(T value){
        if (head.value == value){
            head = head.next;
            return Boolean.TRUE;
        }
        Node<T> searchNode = head;
        Node<T> next = head.next;
        while (next != null){
            if (next.value == value){
                searchNode.next = next.next;
                return true;
            }
            searchNode = next;
            next = next.next;
        }
        return Boolean.FALSE;

    }

    public boolean search(T value){
        if (head == null){
            throw new RuntimeException("linked is empty");
        }else if (head != null && head.value == value){
            return Boolean.TRUE;
        }
        Node<T> searchKey = head.next;
        while (searchKey != null){
            if (searchKey.value == value){
                return Boolean.TRUE;
            }
            searchKey = searchKey.next;
        }

        return Boolean.FALSE;
    }


}
